<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysMessageDetailVo } from '@/api/system/message/SysMessage/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysAccountNameListApi } from '@/api/system/account/SysAccount'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysMessageDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const platformTypeOptions = ref<Array<KeyLabelValueVo>>([])
const getPlatformType = async () => {
  const res = await sysDictChooseListApi('platformType')
  platformTypeOptions.value = res.data
}
getPlatformType()
const moduleNameOptions = ref<Array<KeyLabelValueVo>>([])
const getModuleName = async () => {
  const res = await sysDictChooseListApi('sysModuleName')
  moduleNameOptions.value = res.data
}
getModuleName()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysAccountNameList = ref<Array<KeyLabelValueVo>>([])
const getSysAccountNameList = async () => {
  const res = await selectSysAccountNameListApi({})
  sysAccountNameList.value = res.data
}
getSysAccountNameList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'title',
    label: '标题',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入标题',
      clearable: true
    }
  },
  {
    field: 'content',
    label: '内容',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入内容',
      clearable: true,
      type: 'textarea',
      rows: 15
    }
  },
  {
    field: 'systemSend',
    label: '是否系统发送',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择是否系统发送',
      disabled: true
    }
  },
  {
    field: 'receiveUser',
    label: '接收人',
    component: 'Select',
    componentProps: {
      placeholder: '请选择接收人',
      clearable: true,
      filterable: true,
      options: sysAccountNameList
    }
  },
  {
    field: 'sendUser',
    label: '发送人',
    component: 'Select',
    componentProps: {
      clearable: true,
      filterable: true,
      options: sysAccountNameList,
      disabled: true
    }
  },
  {
    field: 'readState',
    label: '已读状态',
    value: false,
    component: 'Switch',
    componentProps: {
      placeholder: '请选择已读状态',
      disabled: true
    }
  },
  {
    field: 'platformType',
    label: '平台类型',
    value: 'onflow-backend',
    component: 'Select',
    componentProps: {
      placeholder: '请选择平台类型',
      clearable: true,
      filterable: true,
      options: platformTypeOptions
    }
  },
  {
    field: 'moduleName',
    label: '模块名称',
    component: 'Select',
    componentProps: {
      placeholder: '请选择模块名称',
      clearable: true,
      filterable: true,
      options: moduleNameOptions
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '发送时间',
    component: 'Input',
    componentProps: {
      placeholder: '暂无发送时间',
      disabled: true
    }
  },
  {
    field: 'createUserName',
    label: '发送人',
    component: 'Input',
    componentProps: {
      placeholder: '暂无创建人',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  title: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 200) {
          callback(new Error('标题不能超过200个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  content: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 5000) {
          callback(new Error('内容不能超过5000个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  receiveUser: [required()],
  url: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('跳转链接不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  platformType: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('平台类型不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  moduleName: [
    required(),
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 100) {
          callback(new Error('模块名称不能超过100个字符'))
        } else {
          callback()
        }
      }
    }
  ],
  remark: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 500) {
          callback(new Error('备注不能超过500个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

// 所有内容全部都是不可编辑
schema.forEach((item) => {
  item.componentProps.disabled = true
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
